package com.zhongwang.mms.module.jiaGongSemi.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.half.dao.HOrderDao;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.InAndOutService;
import com.zhongwang.mms.module.jiaGongSemi.model.JSemiDetail;
import com.zhongwang.mms.module.jiaGongSemi.service.AccountQueryService;
import com.zhongwang.mms.module.jiaGongSemi.service.StockQueryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 出入库查询页面
 */
@Controller
@RequestMapping("/accountQuery")
public class AccountQueryController extends BaseController {

    @Autowired
    private InAndOutService inAndOutService;
    @Autowired
    private HBasicService HBasicService;
    @Autowired
    private HOrderDao hOrderDao;
    @Autowired
    private StockQueryService stockQueryService;
    @Autowired
    private AccountQueryService accountQueryService;

    @GetMapping("/page")
    public String page(Model model) {

        model.addAttribute("warInfo", stockQueryService.getFinishWarInfo());
        return "jiaGongSemi/accountQuery/page";
    }

    /**
     * 出入库查询
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(JSemiDetail jSemiDetail) {
        startPage();
        List<JSemiDetail> list = accountQueryService.findAll(jSemiDetail);
        return jsonResult(list);
    }

    @GetMapping("/view")
    public String view(@RequestParam(value = "odrId", required = false) Integer odrId, @RequestParam(value = "odrOutType", required = false) Integer odrOutType, @RequestParam(value = "odaMatType", required = false) Integer odaMatType, Model model) {
        model.addAttribute("warInfo", stockQueryService.getFinishWarInfo());
        model.addAttribute("dept", HBasicService.getDept());
        model.addAttribute("order", accountQueryService.findAllById(odrId));
       if ((odrOutType == 1 || odrOutType == 2) && odaMatType == 2) {
            // 毛坯入库
           return  "jiaGongSemi/accountQuery/info/semiInPage";
        }   else if ((odrOutType == 4 ||odrOutType == 7) &&  odaMatType == 2) {
            // 毛坯出库
           return  "jiaGongSemi/accountQuery/info/semiOutPage";
        } else if((odrOutType == 4 || odrOutType == 7) && odaMatType == 4){
            //铝料出库
           return  "jiaGongSemi/accountQuery/info/aluminiumOutPage";
        } else if((odrOutType == 1 || odrOutType == 2) && odaMatType == 4){
            //铝料入库
           return  "jiaGongSemi/accountQuery/info/aluminiumInPage";
        }

        return "";
    }

    /**
     * 查询入库信息
     * @param jSemiDetail
     * @return
     */
    @ResponseBody
    @GetMapping("/getInFinish")
    public JsonResult getInFinish(JSemiDetail jSemiDetail) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(accountQueryService.getInFinish(jSemiDetail));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 导出
     * @param
     * @param param
     */
    @ResponseBody
    @PostMapping("/exportExcel")
    public JsonResult exportExcel(JSemiDetail param) {
       return accountQueryService.exportExcel(param);
    }

    /**
     * 调拨入库查询
     */
    @ResponseBody
    @PostMapping("/allocationList")
    //@RequiresPermissions({"halwar:halwarquery:view"})
    public JsonResult allocationList(HOrderDetailModel param) {
        startPage();
        List<HOrderDetailModel> list = inAndOutService.selectAllocationDetailList(param);
        return jsonResult(list);
    }
}
